package com.blb.edupayservice.controller;

import com.blb.edupayservice.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 微信支付控制器
 */
@Slf4j
@RestController
@RequestMapping("/wxpay")
public class WxPayController {

    @Resource
    private WxPayService wxPayService;

    /**
     * 微信下单
     */
    @RequestMapping("/makeOrder")
    public Map<String,String> makeOrder(Long userId,Long courseId,Long price) throws Exception {
        Map<String, String> map = wxPayService.makeOrder(userId, courseId, price);
        return map;
    }

    /**
     * 生成二维码
     */
    @RequestMapping("/code")
    public void makeQRCode(String code, HttpServletResponse response) throws Exception {
        wxPayService.makeQRCode(code, response);
    }

    /**
     * 检查订单
     * @param tradeNo
     * @return
     * @throws Exception
     */
    @RequestMapping("/checkOrder")
    public String checkOrder(String tradeNo) throws Exception {
        String res = wxPayService.checkOrder(tradeNo);
        return res;
    }

    /**
     * 微信支付回调
     */
    @RequestMapping("/callback")
    public void payCallback(HttpServletRequest request,HttpServletResponse response) throws Exception {
        log.info("微信支付回调成功");
        wxPayService.payCallback(request, response);
    }
}
